import time
import unittest

import requests

from api.login_api import RegLoginApi
from common.utils import common_assert, clear_user_info, read_json
from parameterized import parameterized
from time import sleep


class TestRegLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        clear_user_info()

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        # 封装实例化对象
        self.login_obj = RegLoginApi(self.session)

    def tearDown(self) -> None:
        # 关闭session
        self.session.close()

    # 获取图片验证码测试用例
    @parameterized.expand(read_json("/reg_login_data", "img_verify_code"))
    def test01_img_verify_code(self, r, exp_status_code):
        # 1. 调用封装的方法，获取响应结果
        # 实现接口请求

        resp = self.login_obj.get_img_verify_code(r)
        # 2. 断言

        self.assertEqual(exp_status_code, resp.status_code)

    # 获取短信验证码测试用例
    @parameterized.expand(read_json("/reg_login_data", "phone_verify_code"))
    def test02_phone_verify_code(self, phone, imgVerifyCode, exp_status_code, exp_code, description):
        # 1. 实现获取图片验证码接口
        r = 1
        self.login_obj.get_img_verify_code(r)
        # 2. 实现获取短信验证码接口，获取响应结果
        body = {"phone": phone, "imgVerifyCode": imgVerifyCode, "type": "reg"}
        resp = self.login_obj.get_phone_verify_code(body)
        # json_data = resp.json()
        # 3. 断言
        common_assert(self, resp, exp_status_code, exp_code, description)
        # self.assertEqual(200, resp.status_code)
        # self.assertEqual(200, json_data.get('status'))
        # self.assertIn('成功', json_data.get('description'))

    # 注册测试用例
    @parameterized.expand(read_json("/reg_login_data", "user_register"))
    def test03_register(self, phone, password, imgVerifyCode, phoneVerifyCode, dyServer, invitePhone, exp_status_code, exp_code, description):
        # 1. 实现获取图片验证码接口
        r = 1
        self.login_obj.get_img_verify_code(r)
        # 2. 实现获取短信验证码接口，获取响应结果
        body = {"phone": phone, "imgVerifyCode": "8888", "type": "reg"}
        self.login_obj.get_phone_verify_code(body)
        # 3. 实现注册成功接口，获取响应结果
        body = {"phone": phone, "password": password, "verifycode": imgVerifyCode, "phone_code": phoneVerifyCode,
                "dy_server": dyServer, "invite_phone": invitePhone}
        resp = self.login_obj.user_register(body)
        # json_data = resp.json()
        # 4. 断言
        common_assert(self, resp, exp_status_code, exp_code, description)
        # self.assertEqual(200, resp.status_code)
        # self.assertEqual(200, json_data.get('status'))
        # self.assertIn('成功', json_data.get('description'))

    # 登录测试用例
    @parameterized.expand(read_json("/reg_login_data", "user_login"))
    def test04_user_login(self, keywords, password, exp_status_code, exp_code, description):
        # 实现登陆成功接口，获取响应结果
        body = {"keywords": keywords, "password": password}
        resp = self.login_obj.user_login(body)
        # json_data = resp.json()
        # 断言
        common_assert(self, resp, exp_status_code, exp_code, description)
        # self.assertEqual(200, resp.status_code)
        # self.assertEqual(200, json_data.get('status'))
        # self.assertIn('成功', json_data.get('description'))

    # 密码错误校验测试用例
    @unittest.skip
    def test05_user_login_pwd_error(self):
        # 1.第一次密码错误
        body = {"keywords": "13389898980", "password": "12345"}
        resp1 = self.login_obj.user_login(body)
        # 断言
        common_assert(self, resp1, 200, 100, "密码错误1次")
        # 2.第二次密码错误
        body = {"keywords": "13389898980", "password": "1234567"}
        resp2 = self.login_obj.user_login(body)
        # 断言
        common_assert(self, resp2, 200, 100, "密码错误2次")
        # 3.第三次密码错误
        body = {"keywords": "13389898980", "password": "113456"}
        resp3 = self.login_obj.user_login(body)
        # 断言
        common_assert(self, resp3, 200, 100, "错误密码达到上限")
        # 4.等到60s
        sleep(60)
        # 5.登陆成功
        body = {"keywords": "13389898980", "password": "123456"}
        resp4 = self.login_obj.user_login(body)
        # 断言
        common_assert(self, resp4, 200, 200, "成功")
